import playground from './modules/playground'
import plugins from './modules/plugins'
import components from './modules/components'
// import test from './modules/test'
import { combineURLs } from '@/utils/common'

import layoutHeaderAside from '@/layout/header-aside'

// 由于懒加载页面太多的话会造成webpack热更新太慢，所以开发环境不使用懒加载，只有生产环境使用懒加载
const _import = require('@/libs/util.import.' + process.env.NODE_ENV)

/**
 * 在主框架内显示, 基础路由
 */
const frameIn = [
  {
    path: '/',
    redirect: { name: 'index' },
    icon: 'home',
    component: layoutHeaderAside,
    meta: {
      title: '首页'
    },
    children: [
      // 首页
      {
        path: 'index',
        name: 'index',
        meta: {
          title: '首页',
          auth: true
        },
        component: _import('system/index')
      },
      // 系统 前端日志
      {
        path: 'log',
        name: 'log',
        meta: {
          title: '前端日志',
          auth: true
        },
        component: _import('system/log')
      },
      // 刷新页面 必须保留
      {
        path: 'refresh',
        name: 'refresh',
        hidden: true,
        component: _import('system/function/refresh')
      },
      // 页面重定向 必须保留
      {
        path: 'redirect/:route*',
        name: 'redirect',
        hidden: true,
        component: _import('system/function/redirect')
      }
    ]
  },
  playground,
  plugins,
  components
]

/**
 * 在主框架之外显示
 */
const frameOut = [
  // 登录
  {
    path: '/login',
    name: 'login',
    ignore: true,
    component: _import('system/login')
  }
]

/**
 * 错误页面
 */
const errorPage = [
  {
    path: '*',
    name: '404',
    ignore: true,
    component: _import('system/error/404')
  }
]

// 导出需要显示菜单的
export const frameInRoutes = frameIn
export const asyncRoutes = []
export const constantRoutes = [
  ...frameIn,
  ...frameOut,
  ...errorPage
]

/**
 * 将菜单解析成路由
 * 该函数将会生成一个二级菜单, 策略是 将三级菜单合并到二级路由中, 一级路由保持不变
 */
export function conductRoutes (data) {
  // 遍历第一层路由, 顶层路由
  return data.map(top => {
    // 没有子路由
    if (!top.children || top.children.length < 1) {
      return top
    }
    // 不处理根
    if (top.path === '/') {
      return top
    }
    // 处理二级子路由, 先拿出二层目录的裸菜单, 比如index
    const newChildren = top.children.filter(asideParent => !asideParent.children || asideParent.children.length < 1)

    // 拿出有三层目录的侧栏
    const hasChildAsides = top.children.filter(asideParent => asideParent.children && asideParent.children.length > 0)
    hasChildAsides.forEach(aside => {
      const parentPath = aside.path || ''
      // 遍历aside的三级菜单, 判断二级路由中是否存在
      aside.children.forEach(child => {
        const repeat = newChildren.filter(aside => aside.path === child.path || aside.name === child.name)
        if (repeat.length > 0) {
          console.log('The route failed to load. Please check the route configuration, path, or name conflict')
        }

        // 叠加path top.path
        // child.path = combineURLs(top.path, combineURLs(parentPath, child.path))
        child.path = combineURLs(parentPath, child.path)

        // 加入routes中
        newChildren.push(child)
      })
    })

    // 处理合并后的二级路由, 拼接出绝对路径
    newChildren.forEach(route => {
      route.path = combineURLs(top.path, route.path)
    })
    const newTop = {
      ...top,
      children: newChildren
    }
    return newTop
  })
}

// 将constantRoutes菜单解析成路由
export const conductRoutesLet = conductRoutes(constantRoutes)

// 重新组织后导出
export default conductRoutesLet
